
package com.elite.groovy.callback
import com.elite.groovy.util.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.json.JSONObject;
/**
 * @use 标准工单保单服务、理赔服务工单办结后发送短信
 * @author chopper
 *
 */
public class TSremindSMS implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(TSremindSMS.class);
	private String charset = "utf-8"; //设置字符集
	private int timeout = 100000; //设置请求超时时间
	private int enableSSLflag = 0;	
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		JSONObject result = new JSONObject();
        log.debug("-----投诉提醒发送短信----------");
		try {
			//机构提醒短信
			String tssql="select * from (select o.workorderid,o.objective_guid,o.handlegroup_id from tabcorder_accuse ta left join objective o on ta.relation_id = o.objective_guid where to_char(ta.waring_time,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd') and  o.curstep_id_map in('SS237G','JO826X') union select o.workorderid,o.objective_guid,o.handlegroup_id from tabcorder_zts zts left join objective o on zts.relation_id = o.objective_guid where substr(zts.warning_time,0,10)=to_char(sysdate,'yyyy-mm-dd') and o.curstep_id_map in ('MMB584','O9Y03F') and o.source is null union select o.workorderid,o.objective_guid,o.handlegroup_id from tabcorder_zts zts left join objective o on zts.relation_id = o.objective_guid where substr(zts.warning_time,0,10)=to_char(sysdate,'yyyy-mm-dd') and o.curstep_id_map in ('MMB584','O9Y03F') and o.source = 'GW')aa order by aa.handlegroup_id";
			def tslist=eliteBean.chaxun_sql(tssql,"dataSource");
			log.debug("机构提醒单量："+tslist.size());

			//短信模板
			def documentsql="select documentpath,to_char(sysdate+1,'yyyy-mm-dd') endtime from DOCTEMPLATE where modelid='TSREMIND'";
			def documentlist=eliteBean.chaxun_sql(documentsql,"dataSource");
			def documentpaths=documentlist[0].get("documentpath");
			def endtime = documentlist[0].get("endtime").toString();
			documentpaths = documentpaths.toString().replace("{times}",endtime);

			String documentpath= "";
			String groupid = "";
			String orderids = "";

			for(def i=0;i<tslist.size();i++){
				String handlegroup_id = tslist[i].get("handlegroup_id").toString();
				if(!"".equals(handlegroup_id)&&!groupid.equals(handlegroup_id)){
					//先发邮件
					if(!"".equals(groupid)&&orderids.length()>0){
						def mobilesql = "select parameter_desc from custom_parameter " +
								"where PARAMETER_TYPE = 'CBYYORG' and PARAMETER_ID =  '"+groupid+"'";
						def mobilelist=eliteBean.chaxun_sql(mobilesql,"dataSource");
						def mobile=mobilelist[0].get("parameter_desc");
						orderids = orderids.substring(0,orderids.length()-1);
						String[] oids = orderids.split(",");
						def num = oids.length;
						documentpath = documentpaths.toString().replace("{num}",num.toString()).replace("{orderIds}",orderids);

						log.debug("短信内容："+documentpaths);
						def sendsql="Insert Into Documentout(Documentout_Guid,documentfrom,Doctype,Status,Trialcount,Trialcountlimit,Documentpath,Handleby_Id,Handlegroup_Id,Createdby,Createddate,Expectedstartdate,Documentto) Values(sys_guid(),1,3,0,0,5,'"+documentpath+"','kfstem','SYSTEM','kfstem',sysdate,sysdate,'"+mobile+"')"
						eliteBean.update(sendsql,"dataSource");
					}
					//更新处理机构
					groupid = handlegroup_id;
					orderids = tslist[i].get("workorderid").toString()+",";
				}else{
					//短信文本往后添加
					orderids = orderids + tslist[i].get("workorderid").toString()+",";
				}

				if(i==tslist.size()-1){
					//最后一条发邮件
					groupid = handlegroup_id;
					orderids = tslist[i].get("workorderid").toString()+",";
					if(!"".equals(groupid)&&orderids.length()>0){
						def mobilesql = "select parameter_desc from custom_parameter " +
								"where PARAMETER_TYPE = 'CBYYORG' and PARAMETER_ID =  '"+groupid+"'";
						def mobilelist=eliteBean.chaxun_sql(mobilesql,"dataSource");
						def mobile=mobilelist[0].get("parameter_desc");
						orderids = orderids.substring(0,orderids.length()-1);
						String[] oids = orderids.split(",");
						def num = oids.length;
						documentpath = documentpaths.toString().replace("{num}",num.toString()).replace("{orderIds}",orderids);
						log.debug("短信内容："+documentpaths);
						def sendsql="Insert Into Documentout(Documentout_Guid,documentfrom,Doctype,Status,Trialcount,Trialcountlimit,Documentpath,Handleby_Id,Handlegroup_Id,Createdby,Createddate,Expectedstartdate,Documentto) Values(sys_guid(),1,3,0,0,5,'"+documentpath+"','kfstem','SYSTEM','kfstem',sysdate,sysdate,'"+mobile+"')"
						eliteBean.update(sendsql,"dataSource");
					}
				}
			}

			//投诉管理岗提醒短信（还需要加上未关闭未办结的条件，名称都+1）
			String tssql1="select * from (select o.workorderid from tabcorder_accuse ta left join objective o on ta.relation_id = o.objective_guid where to_char(ta.accept_time,'yyyy-mm-dd')=to_char(sysdate-12,'yyyy-mm-dd') and o.bzgdflag <>'2' and o.objectivestatus<>'CLOSE' and o.objectivestatus<>'FAIL' union select o.workorderid from tabcorder_zts zts left join objective o on zts.relation_id = o.objective_guid where substr(zts.reception_time,0,10)=to_char(sysdate-12,'yyyy-mm-dd') and o.bzgdflag <>'2' and o.objectivestatus<>'CLOSE' and o.objectivestatus<>'FAIL' and o.source is null union select o.workorderid from tabcorder_zts zts left join objective o on zts.relation_id = o.objective_guid where substr(zts.reception_time,0,10)=to_char(sysdate-12,'yyyy-mm-dd') and o.bzgdflag <>'2' and o.objectivestatus<>'CLOSE' and o.objectivestatus<>'FAIL' and o.source = 'GW')aa ";
			def tslist1=eliteBean.chaxun_sql(tssql1,"dataSource");
			log.debug("机构提醒单量："+tslist1.size());

			//短信模板
			def documentsql1="select documentpath,to_char(sysdate+3,'yyyy-mm-dd') endtime from DOCTEMPLATE where modelid='TSREMIND2'";
			def documentlist1=eliteBean.chaxun_sql(documentsql1,"dataSource");
			def documentpaths1=documentlist1[0].get("documentpath");
			def endtime1 = documentlist1[0].get("endtime").toString();
			documentpaths1 = documentpaths1.toString().replace("{times}",endtime1);

			String documentpath1= "";
			String groupid1 = "";
			String orderids1 = "";

			for(def i=0;i<tslist1.size();i++){
				//拼接工单号
				orderids1 = orderids1 + tslist1[i].get("workorderid").toString()+",";
				//最后一条发短信
				if(i==tslist1.size()-1){
					//获取投诉审核岗ORG016手机号
					def mobilesql1 = "select parameter_desc from custom_parameter where PARAMETER_TYPE = 'CBYYORG' and PARAMETER_ID = 'ORG016' ";
					def mobilelist1=eliteBean.chaxun_sql(mobilesql1,"dataSource");
					def mobile1=mobilelist1[0].get("parameter_desc");
					//拼接短信内容
					orderids1 = orderids1.substring(0,orderids1.length()-1);
					String[] oids1 = orderids1.split(",");
					def num1 = oids1.length;
					documentpath1 = documentpaths1.toString().replace("{num}",num1.toString()).replace("{orderIds}",orderids1);
					log.debug("短信内容："+documentpaths1);
					//发送短信
					def sendsql1="Insert Into Documentout(Documentout_Guid,documentfrom,Doctype,Status,Trialcount,Trialcountlimit,Documentpath,Handleby_Id,Handlegroup_Id,Createdby,Createddate,Expectedstartdate,Documentto) Values(sys_guid(),1,3,0,0,5,'"+documentpath1+"','kfstem','SYSTEM','kfstem',sysdate,sysdate,'"+mobile1+"')"
					eliteBean.update(sendsql1,"dataSource");
				}
			}
			result.put("code","1");
		}catch(Exception e) {
			result.put("code", "-1");
			result.put("msg", "fail");
		}
		return "1";
	}	


}
